<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="title" label="标题" show-overflow-tooltip>
        <template slot-scope="{ row }">
          <el-tag
            class="tag"
            size="mini"
            effect="dark"
            type="danger"
            v-if="row.orderBy != 0"
            disable-transitions
            >置顶</el-tag
          >
          <el-tag
            class="tag"
            size="mini"
            effect="dark"
            type="warning"
            v-if="row.selection != 0"
            disable-transitions
            >精选</el-tag
          >
          <span class="txt-hover" @click="handleDetail(row)">
            {{ row.title }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="classifyId"
        label="分类"
        width="120"
        show-overflow-tooltip=""
      >
        <template slot-scope="{ row }">
          <span>{{ row.classifyId ? row.sysClassify.name : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="发布状态" width="100">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            active-value="1"
            inactive-value="0"
            @change="handleStatus(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="审核状态" width="100">
        <template slot-scope="{ row }">
          <span v-if="row.auditStatus == 1" class="color-info">待审核</span>
          <span v-if="row.auditStatus == 2" class="color-success">已通过</span>
          <span v-if="row.auditStatus == 3" class="color-danger">未通过</span>
          <el-popover
            placement="top"
            title="审核说明"
            width="300"
            trigger="hover"
            style="margin-left: 5px"
            v-if="auditReason(row)"
            :content="auditReason(row)"
          >
            <i class="el-icon-info" slot="reference"></i>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建人"
        width="100"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">
          {{ row.sysUser.userName }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="{ row }">
          <el-button
            type="success"
            size="mini"
            @click="handleDetail(row)"
            style="margin-right: 10px"
            >详情</el-button
          >
          <el-dropdown
            split-button
            type="primary"
            size="mini"
            @click="handleEdit(row)"
          >
            编辑
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="handleAudit(row)"
                :disabled="row.status == 0"
                >审核</el-dropdown-item
              >
              <el-dropdown-item
                @click.native="handleSelection(row)"
                :disabled="row.status == 0 || row.auditStatus != 2"
              >
                {{ row.selection == 0 ? '设为精选' : '取消精选' }}
              </el-dropdown-item>
              <el-dropdown-item
                @click.native="handleTop(row)"
                :disabled="row.status == 0 || row.auditStatus != 2"
              >
                {{ row.orderBy == 0 ? '设为置顶' : '取消置顶' }}
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(row)"
                >删除</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import setInfo from '@/mixins/setInfo'
export default {
  mixins: [setInfo],
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        isAll: 'Y',
      },
      tableData: [],
      pageObj: {},
    }
  },
  computed: {
    auditReason() {
      return (row) => {
        if (!row.extFiled) return
        let extFiled = JSON.parse(row.extFiled)
        if (!extFiled.hasOwnProperty('auditReason')) return
        return extFiled.auditReason
      }
    },
  },
  mounted() {
    this.getList()
    this.$bus.$on('refreshBbsTopicList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    this.$bus.$off('refreshBbsTopicList')
  },
  methods: {
    async getList() {
      let res = await this.$store.dispatch('queryBbsTopic', this.listQuery)
      this.tableData = res.data
      this.pageObj = res
    },
    handleDetail(row) {
      const URL = this.$router.resolve({
        name: 'bbs-detail',
        query: { id: row.id },
      })
      window.open(URL.href, '_blank')
    },
    handleEdit(row) {
      this.$bus.$emit('showDialogBbsAdd', row)
    },
    //审核
    handleAudit(row) {
      this.$bus.$emit('bbsAudit', row)
    },
    //精选
    async handleSelection(row) {
      let { id, selection } = row
      selection = selection == 0 ? 1 : 0
      await this.$store.dispatch('updateBbsTopic', { id, selection })
      this.getList()
    },
    //置顶
    async handleTop(row) {
      let { id, orderBy } = row
      if (orderBy == 0) {
        this.$bus.$emit('setBbsTop', row)
      } else {
        await this.$store.dispatch('updateBbsTopic', { id, orderBy: 0 })
        this.getList()
      }
    },
    async handleDelete(row) {
      await this.$store.dispatch('delBbsTopic', row)
      this.getList()
    },
    //发布
    async handleStatus(row) {
      let { id, status } = row
      await this.$store.dispatch('updateBbsTopic', { id, status })
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss">
.tag {
  margin-right: 5px;
}
</style>